package com.demo.studentmanager;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(@NonNull Context context) {
        super(context, "student_", null, 1);
    }

    public DBHelper(@NonNull Fragment context) {
        super(context.requireContext(), "student_", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists user(phone text primary key,name text,sex text,clazz text,password varchar(10))");
        //课程表 课程名称,教师,id
        db.execSQL("create table if not exists course(name text,teacher text,id integer primary key autoincrement)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void addCourse(String name, String teacher) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("teacher", teacher);
        writableDatabase.insert("course", null, values);
    }

    @SuppressLint("Range")
    public List<Course> getCourse(String name) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from course where name like ?", new String[]{"%" + name + "%"});
        List<Course> courses = new ArrayList<>();
        while (cursor.moveToNext()) {
            Course course = new Course();
            course.setName(cursor.getString(cursor.getColumnIndex("name")));
            course.setTeacher(cursor.getString(cursor.getColumnIndex("teacher")));
            course.setId(cursor.getInt(cursor.getColumnIndex("id")));
            courses.add(course);
        }
        return courses;
    }

    public boolean register(User user) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("phone", user.getAccount());
        values.put("clazz", user.getClazz());
        values.put("sex", user.getSid());
        values.put("password", user.getPassword());
        return writableDatabase.insert("user", null, values) > 0;
    }

    @SuppressLint("Range")
    public boolean login(String phone, String password) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from user where phone = ? and password = ?", new String[]{phone, password});
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        return false;
    }

    public void updateUser(User user) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("clazz", user.getClazz());
        values.put("sex", user.getSid());
        values.put("password", user.getPassword());
        writableDatabase.update("user", values, "phone = ?", new String[]{user.getPassword()});
    }

    public void deleteUser(User user) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        writableDatabase.delete("user", "phone = ?", new String[]{user.getAccount()});
    }

    @SuppressLint("Range")
    public List<User> getUser(String name) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from user where name like ?", new String[]{"%" + name + "%"});
        List<User> users = new ArrayList<>();
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                User user = new User();
                user.setName(cursor.getString(cursor.getColumnIndex("name")));
                user.setAccount(cursor.getString(cursor.getColumnIndex("phone")));
                user.setClazz(cursor.getString(cursor.getColumnIndex("clazz")));
                user.setSid(cursor.getString(cursor.getColumnIndex("sex")));
                user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                users.add(user);
            }
        }
        return users;
    }

    @SuppressLint("Range")
    public User getUserByPhone(String phone) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        Cursor cursor = writableDatabase.rawQuery("select * from user where phone = ?", new String[]{phone});
        List<User> users = new ArrayList<>();
        if (cursor.getCount() > 0) {
            if (cursor.moveToNext()) {
                User user = new User();
                user.setName(cursor.getString(cursor.getColumnIndex("name")));
                user.setAccount(cursor.getString(cursor.getColumnIndex("phone")));
                user.setClazz(cursor.getString(cursor.getColumnIndex("clazz")));
                user.setSid(cursor.getString(cursor.getColumnIndex("sex")));
                user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                return user;
            }
        }
        return null;
    }

    public void updateCourse(Course course) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", course.getName());
        values.put("teacher", course.getTeacher());
        writableDatabase.update("course", values, "id = ?", new String[]{String.valueOf(course.getId())});
    }

    public void addCourse(Course course) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", course.getName());
        values.put("teacher", course.getTeacher());
        writableDatabase.insert("course", null, values);
    }

    public void deleteCourse(Course course) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        writableDatabase.delete("course", "id = ?", new String[]{String.valueOf(course.getId())});
    }
}
